<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <title>Hello, world!</title>
    <style>
        textarea {
            display: block;
            width: 100%;
            padding: 6px;
            font-family: 微软雅黑;
            line-height: 25px;
            font-size: small;
            resize: none;
            overflow-x: hidden;
            overflow-y: auto;
        }
        div {
            width: 95%;
            overflow: hidden;
            min-height: 20px;
            padding: 5px;
        }
        #codeResult:focus,#val:focus {
            outline: none;
            border: none;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col">
            <div class="row">
                <h2 class="col-8">源代码</h2>
                <button type="button" class="btn btn-success col-4" onclick="run()" id="run">点击运行</button>
                <button type="button" class="btn btn-warning col-4" onclick="run()" style="display: none" id="run2">重新运行</button>
            </div>
            <div class="row">
                <textarea id="codeArea" rows="20"></textarea>
            </div>
        </div>
        <div class="col-1"></div>
        <div class="col">
            <div class="row">
                <h2>运行结果</h2>
            </div>
            <div class="row" style="border: solid 1px #c4c4c4;margin-top: 6px;">
                <textarea contenteditable="true" id="codeResult" style="border: none" disabled></textarea>
                <input type="text"  id="val" style="padding-bottom: 250px;border: none;margin-top: -10px; width: 100%" />
<!--                <button type="button" class="btn btn-success" onclick="subVal()">提交</button>-->
            </div>
        </div>
    </div>
</div>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script
    src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.15.0/umd/popper.min.js"
    integrity = "sha384-L2pyEeut/H3mtgCBaUNw7KWzp5n9+4pDQiExs933/5QfaTh8YStYFFkOzSoXjlTb"
    crossorigin = "anonymous" ></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
<script>
    let codeId = undefined
    var change = undefined
    $("#val").hide()
    function run() {
        $.ajax({
            url: "/getResult",
            type: 'post',
            data: {java_source: $("#codeArea").val()},
            success: function (data) {
                let datas = data.split('--<>--');
                $("#codeResult").val(datas[0])
                codeId = datas[1]
                $("#run").hide();
                $("#run2").show();
                change()
                $("#val").val('')
                if(codeId != undefined){
                    $("#val").show()
                    $("#val").focus()
                }
            }
        });
    }

    $("#val").keypress(function (e) {
        if (e.which == 13) {
            subVal()
        }
    });
    function subVal() {
        if(codeId == undefined){
            return
        }
        let val = $("#val").val()
        $("#val").val('')
        $("#val").blur()
        $("#val").hide()
        $.ajax({
            url: "/getResult",
            type: 'post',
            data: {codeId: codeId,subVal: val},
            success: function (data) {
                let datas = data.split('--<>--');
                $("#codeResult").val($("#codeResult").val()+'\n'+val+'\n'+datas[0])
                codeId = datas[1]
                change()
                console.log(codeId)
                if(codeId != undefined){
                    $("#val").focus()
                    $("#val").show()
                }
            }
        });
    }



    var autoTextarea = function (elem, extra, maxHeight) {
        extra = extra || 0;
        var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,
            isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera'),
            addEvent = function (type, callback) {
                elem.addEventListener ?
                    elem.addEventListener(type, callback, false) :
                    elem.attachEvent('on' + type, callback);
            },
            getStyle = elem.currentStyle ? function (name) {
                var val = elem.currentStyle[name];

                if (name === 'height' && val.search(/px/i) !== 1) {
                    var rect = elem.getBoundingClientRect();
                    return rect.bottom - rect.top -
                        parseFloat(getStyle('paddingTop')) -
                        parseFloat(getStyle('paddingBottom')) + 'px';
                };

                return val;
            } : function (name) {
                return getComputedStyle(elem, null)[name];
            },
            minHeight = parseFloat(getStyle('height'));

        elem.style.resize = 'none';

        change = function () {
            var scrollTop, height,
                padding = 0,
                style = elem.style;

            if (elem._length === elem.value.length) return;
            elem._length = elem.value.length;

            if (!isFirefox && !isOpera) {
                padding = parseInt(getStyle('paddingTop')) + parseInt(getStyle('paddingBottom'));
            };
            scrollTop = document.body.scrollTop || document.documentElement.scrollTop;

            elem.style.height = minHeight + 'px';
            if (elem.scrollHeight > minHeight) {
                if (maxHeight && elem.scrollHeight > maxHeight) {
                    height = maxHeight - padding;
                    style.overflowY = 'auto';
                } else {
                    height = elem.scrollHeight - padding;
                    style.overflowY = 'hidden';
                };
                style.height = height + extra + 20 + 'px';
                scrollTop += parseInt(style.height) - elem.currHeight;
                document.body.scrollTop = scrollTop;
                document.documentElement.scrollTop = scrollTop;
                elem.currHeight = parseInt(style.height);
            };
        };
        change();
    };

    var text = document.getElementById("codeResult");
    autoTextarea(text);// 调用
</script>


</body>
</html>